package com.allwees.bs.module.pms.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.allwees.bs.module.pms.entity.Sku;
import com.allwees.bs.module.pms.req.query.ExportGaSkuQueryReq;
import com.allwees.bs.module.pms.req.query.ProductQueryReq;
import com.allwees.bs.module.pms.req.query.ProductsSkusQueryReq;
import com.allwees.bs.module.pms.req.query.SkuQueryReq;
import com.allwees.bs.module.pms.vo.ExportGaSkuVo;
import com.allwees.bs.module.pms.vo.ExportProductSkuBean;
import com.allwees.core.dao.annotation.NoDataPermission;
import com.allwees.core.dao.mybatis.dao.BaseDao;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author yangylsky
 * @since 2019-08-21 09:26
 */
public interface SkuDao extends BaseDao<Sku> {
    List<Sku> findList(SkuQueryReq skuQueryReq);

    default int getSkuCount(String productUuid) {
    	return selectCount(new QueryWrapper<Sku>().eq("product_uuid", productUuid));
    }

    void updateAppendQuantity(@Param("uuid") String skuUuid, Integer quantity);

    List<Sku> findHappenOrderList(@Param("req") ProductQueryReq req);

	@Select({
		"SELECT DISTINCT s.custom_no",
		"FROM prod_sku s",
		"INNER JOIN prod_product p",
		" ON s.product_uuid=p.`uuid` ",
		"WHERE p.retailer_uuid=#{retailerUuid}",
		" AND s.custom_no is not null"
	})
	List<String> findAllCustomNosByRetailerUuid(String retailerUuid);

    default Map<String, Sku> selectByProductUuid(String uuid) {
        if (StringUtils.isBlank(uuid)) {
            return null;
        }

        QueryWrapper<Sku> wrapper = new QueryWrapper<>();
        wrapper.eq("product_uuid", uuid);
        List<Sku> skus = selectList(wrapper);
        if(CollectionUtils.isEmpty(skus)){
            return new HashMap<>(0);
        }
        return skus.stream().collect(Collectors.toMap(Sku::getUuid,o->o));
    }

	List<ExportProductSkuBean> queryProductsSkus(@Param("page") Page<ExportProductSkuBean> page, @Param("req") ProductsSkusQueryReq req);

	@NoDataPermission
	List<ExportGaSkuVo> queryGaSkus(@Param("req") ExportGaSkuQueryReq req);

	@Select({"select s.id,p.`retailer_uuid` uuid,s.`photos_str`,s.created_at from prod_sku s left join prod_product p on s.`product_uuid`=p.`uuid` where s.`photos_str` != ''"})
	List<Sku> queryImgs();
}
